package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.AhhcMedicalStaff;
import com.ruoyi.system.mapper.AhhcDepartmentsMapper;
import com.ruoyi.system.domain.AhhcDepartments;
import com.ruoyi.system.service.IAhhcDepartmentsService;

/**
 * 科室表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-14
 */
@Service
public class AhhcDepartmentsServiceImpl implements IAhhcDepartmentsService 
{
    @Autowired
    private AhhcDepartmentsMapper ahhcDepartmentsMapper;

    /**
     * 查询科室表
     * 
     * @param id 科室表主键
     * @return 科室表
     */
    @Override
    public AhhcDepartments selectAhhcDepartmentsById(String id)
    {
        return ahhcDepartmentsMapper.selectAhhcDepartmentsById(id);
    }

    /**
     * 查询科室表列表
     * 
     * @param ahhcDepartments 科室表
     * @return 科室表
     */
    @Override
    public List<AhhcDepartments> selectAhhcDepartmentsList(AhhcDepartments ahhcDepartments)
    {
        return ahhcDepartmentsMapper.selectAhhcDepartmentsList(ahhcDepartments);
    }

    /**
     * 新增科室表
     * 
     * @param ahhcDepartments 科室表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAhhcDepartments(AhhcDepartments ahhcDepartments)
    {
        int rows = ahhcDepartmentsMapper.insertAhhcDepartments(ahhcDepartments);
        insertAhhcMedicalStaff(ahhcDepartments);
        return rows;
    }

    /**
     * 修改科室表
     * 
     * @param ahhcDepartments 科室表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAhhcDepartments(AhhcDepartments ahhcDepartments)
    {
        ahhcDepartmentsMapper.deleteAhhcMedicalStaffByDepartmentId(ahhcDepartments.getId());
        insertAhhcMedicalStaff(ahhcDepartments);
        return ahhcDepartmentsMapper.updateAhhcDepartments(ahhcDepartments);
    }

    /**
     * 批量删除科室表
     * 
     * @param ids 需要删除的科室表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAhhcDepartmentsByIds(String[] ids)
    {
        ahhcDepartmentsMapper.deleteAhhcMedicalStaffByDepartmentIds(ids);
        return ahhcDepartmentsMapper.deleteAhhcDepartmentsByIds(ids);
    }

    /**
     * 删除科室表信息
     * 
     * @param id 科室表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAhhcDepartmentsById(String id)
    {
        ahhcDepartmentsMapper.deleteAhhcMedicalStaffByDepartmentId(id);
        return ahhcDepartmentsMapper.deleteAhhcDepartmentsById(id);
    }

    /**
     * 新增医护管理信息
     * 
     * @param ahhcDepartments 科室表对象
     */
    public void insertAhhcMedicalStaff(AhhcDepartments ahhcDepartments)
    {
        List<AhhcMedicalStaff> ahhcMedicalStaffList = ahhcDepartments.getAhhcMedicalStaffList();
        String id = ahhcDepartments.getId();
        if (StringUtils.isNotNull(ahhcMedicalStaffList))
        {
            List<AhhcMedicalStaff> list = new ArrayList<AhhcMedicalStaff>();
            for (AhhcMedicalStaff ahhcMedicalStaff : ahhcMedicalStaffList)
            {
                ahhcMedicalStaff.setDepartmentId(id);
                list.add(ahhcMedicalStaff);
            }
            if (list.size() > 0)
            {
                ahhcDepartmentsMapper.batchAhhcMedicalStaff(list);
            }
        }
    }
}
